package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.RegexUtils;

public class XiziBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://bbs.xizi.com/thread-[0-9]*-1-1.html",
				"http://bbs.xizi.com/read.php[?].*tid=[0-9]*.*"
		};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		String autohr = null;

		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;

		Document doc = Jsoup.parse(htmltxt);

		title = JSoupUtils.select(doc,"h1#subject_tpc", ".post_title");
		date = DateUtils.matchDate(doc.select("em.s3").text());
		if (date ==null){
			date = DateUtils.matchDate(doc.select(".date").text());
		}
		doc.select("span.user_name .follow_btn").remove();
		doc.select("dd.username .follow_btn").remove();
		Element first = doc.select("dd.username").first();
		if (first!=null)
		{
			autohr = first.text();
		}
		if (StringUtils.isBlank(autohr)){
			first = doc.select("span.user_name").first();
			if (first!=null){
				autohr =first.text();
			}
		}
		content = HtmlCleaner.getContentHtml(url,  doc.select("div.tpc_content"));
		if (StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,  doc.select(".post_content"));
		}

		if(doc.select("span.popularity em").size()==2){
			clickNum = NumberUtils.matchNumber(doc.select("span.popularity em").get(0).text());
			commnetNum =NumberUtils.matchNumber(doc.select("span.popularity em").get(1).text());
		}
		if (commnetNum==null && clickNum==null){
			commnetNum = NumberUtils.matchNumber(doc.select("span.replies").text());
			clickNum = NumberUtils.matchNumber(doc.select("span.hits").text());
		}
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setAuthor(autohr);
		/**
		 * 解析用于更新的地址
		 */
		bbs.setUpdateUrl(url);
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commnetNum);
		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if (StringUtils.isNotBlank(updateUrl)){
				UrlMeta urlMeta = CrawlHTML.responseToURL(updateUrl);
				return parserHtml(urlMeta);
			}
			return null;
		}
		return null;
	}

	public static void main(String[] args) {
		XiziBbsAnalyse a = new XiziBbsAnalyse();

		String url = "http://bbs.xizi.com/thread-3714338-1-1.html#2317333";
		System.out.println(DoMainUtils.GetDomainName(url));
		if (!a.isDetailPage(url)) {
			System.out.println("不匹配规则");
		} else {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
//			System.out.println(a.Update(parserHtml));
		}

	}
	
}
